import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:hx_im_plugin/hx_im_plugin.dart';
import 'package:hx_im_plugin_example/hq_im_ui/pages/group/hq_group_items/hq_group_members_menu.dart';
import 'package:hx_im_plugin_example/hq_im_ui/widgets/hq_app_bar.dart';
import 'package:hx_im_plugin_example/hq_im_ui/widgets/hq_common_widgets.dart';

class HqGroupInfoPage extends StatefulWidget {

  final EMGroup group;
  HqGroupInfoPage(this.group);

  @override
  _HqGroupInfoPageState createState() => _HqGroupInfoPageState();
}

class _HqGroupInfoPageState extends State<HqGroupInfoPage> {

  EMGroup _group;
  EMConversation _conv;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 500))
        .then((value) => _fetchGroupInfo());
  }

  @override
  Widget build(BuildContext context) {
    bool needApproval = false;
    if (_group != null) {
      if (_group.settings?.style == EMGroupStyle.PublicJoinNeedApproval) {
        needApproval = true;
      } else {
        needApproval = false;
      }
    }
    return Scaffold(
      appBar: HqAppBar(
        '群组信息',
        rightWidgets: [
          // FlatButton(
          //   onPressed: () =>
          //   needApproval ? _approvalJoinPublicGroup() : _joinPublicGroup(),
          //   child: Builder(
          //     builder: (_) {
          //       String text = '';
          //       if (_group != null) {
          //         text = needApproval ? '申请' : '加入';
          //       }
          //       return Text(
          //         text,
          //         style: TextStyle(
          //           color: Colors.white,
          //           fontSize: sFontSize(16),
          //         ),
          //       );
          //     },
          //   ),
          // )
        ],
      ),
      body: Container(
        // margin: EdgeInsets.all(
        //   sWidth(10),
        // ),
        child: Column(
          children: [
            /// 群成员
            _conv == null ? Container() :
            Card(
              color: Colors.white, //颜色
              margin: EdgeInsets.all(sWidth(10)), //margin
              child: HqGroupMembersMenu(_conv),
            ),
            Card(
              color: Colors.white, //颜色
              margin: EdgeInsets.all(sWidth(10)), //margin
              child: Container(
                margin: EdgeInsets.all(sWidth(10)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Expanded(
                          child: Text(
                            widget.group.name,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: sFontSize(16),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: sHeight(5),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Expanded(
                          child: Text(
                            widget.group.groupId,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: sFontSize(13),
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTapUp: (_) => _pushGroupMembersInfo(),
              child: Card(
                color: Colors.white, //颜色
                margin: EdgeInsets.all(sWidth(10)), //margin
                child: Container(
                  margin: EdgeInsets.all(
                    sWidth(10),
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: Text('群主'),
                      ),
                      Text(_group?.owner ?? ''),
                    ],
                  ),
                ),
              ),
            ),
            Card(
              color: Colors.white, //颜色
              margin: EdgeInsets.all(sWidth(10)), //margin
              child: Container(
                margin: EdgeInsets.all(
                  sWidth(10),
                ),
                child: Row(
                  children: [
                    Expanded(
                      child: Text('成员数'),
                    ),
                    Text(_group != null ? _group?.memberCount.toString() : ''),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _joinPublicGroup() {
    try {
      SmartDialog.showLoading(msg: '加入中...');
      EMClient.getInstance.groupManager.joinPublicGroup(_group.groupId);
      SmartDialog.showToast('加入成功');
    } on EMError catch (e) {
      SmartDialog.showToast('加入失败: $e');
    } finally {
      SmartDialog.dismiss();
    }
  }

  _approvalJoinPublicGroup() {
    try {
      SmartDialog.showLoading(msg: '申请中...');
      EMClient.getInstance.groupManager
          .requestToJoinPublicGroup(_group.groupId);
      SmartDialog.showToast('申请已发送');
    } on EMError catch (e) {
      SmartDialog.showToast('申请失败: $e');
    } finally {
      SmartDialog.dismiss();
    }
    EMClient.getInstance.groupManager.requestToJoinPublicGroup(_group.groupId);
  }

  _fetchGroupInfo() async {
    try {
      SmartDialog.showLoading(msg: '获取中...');
      _group = await EMClient.getInstance.groupManager
          .getGroupSpecificationFromServer(widget.group.groupId);
      _conv = await EMClient.getInstance.chatManager.getConversation(widget.group.groupId);
      setState(() {});
      SmartDialog.showToast('获取成功');
    } on EMError catch (e) {
      SmartDialog.showToast('获取失败: $e');
    } finally {
      SmartDialog.dismiss();
    }
  }

  _pushGroupMembersInfo() {
    // Navigator.of(context).pushNamed('/groupMemberList', arguments: [
    //   _group,
    //   GroupMembersPageType.Admin,
    // ]);
  }
}

